package cn.toma.common.utils;


import cn.toma.common.vo.FfBootConstant;
import cn.toma.common.vo.QueryRequest;
import cn.toma.common.vo.StringPool;
import com.github.pagehelper.Page;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

public class StringUtils extends org.apache.commons.lang3.StringUtils
{
	public static final String ZERO = "0";
	public static final String spot = ".";
	public static final String RESULT = "result";

	/**
	 * 新增操作
	 */
	public static final String ADD = "ADD";

	/**
	 * 删除操作
	 */
	public static final String DELETE = "DELETE";

	/**
	 * 修改操作
	 */
	public static final String UPDATE = "UPDATE";

	/**
	 * 删除状态
	 */
	public static final String STATUS_DELETE = "DELETE";

	/**
	 * 使用状态
	 */
	public static final String STATUS_ACTIVE = "ACTIVE";

	/**
	 * 电话格式验证
	 **/
	@SuppressWarnings("unused")
	private static final String PHONE_CALL_PATTERN = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?$";

	/**
	 * 中国电信号码格式验证 手机段： 133,153,180,181,189,177,1700
	 **/
	private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";

	/**
	 * 中国联通号码格式验证 手机段：130,131,132,155,156,185,186,145,176,1709,166
	 **/
	private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|6[6]|7[6]|8[56]|9[9])\\d{8}$)|(^1709\\d{7}$)";

	/**
	 * 中国移动号码格式验证
	 * 手机段：134,135,136,137,138,139,150,151,152,157,158,159,182,183,184
	 * ,187,188,147,178,1705
	 **/
	private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[0-9]|5[0-27-9]|6[0-9]|7[0-9]|8[2-478]|9[0-9])\\d{8}$)|(^1705\\d{7}$)";

	/**
	 * 判断一个字符是否是中文 @Description: @param: @param c
	 * 需要校验的字节码 @param: @return @return: boolean @throws
	 */
	public static boolean isChinese(char c)
	{
		return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
	}

	/**
	 * 判断一个字符串是否含有中文 @Description: @param: @param str
	 * 需要校验的字符串 @param: @return @return: boolean @throws
	 */
	public static boolean isChinese(String str)
	{
		if (str == null)
			return false;
		for (char c : str.toCharArray()) {
			if (isChinese(c))
				return true;// 有一个中文字符就返回
		}
		return false;
	}

	/**
	 * 校验格式是否为邮箱
	 *
	 * @param str
	 * @return
	 */
	public static boolean checkMail(String str)
	{
		if (str != null && str.indexOf(".") > 0 && str.indexOf("@") > 0 && str.length() - str.indexOf(".") > 1
				&& str.indexOf(".") - str.indexOf("@") > 1)
		{
			return true;
		}
		return false;
	}

	/**
	 * 校验格式是否为手机号
	 *
	 * @param str
	 * @return
	 */
	public static boolean checkPhone(String str)
	{
		boolean result = false;
		if (str != null && str.length() == 11) {
			boolean result1 = str == null || str.trim().equals("") ? false : match(CHINA_MOBILE_PATTERN, str);
			boolean result2 = str == null || str.trim().equals("") ? false : match(CHINA_UNICOM_PATTERN, str);
			boolean result3 = str == null || str.trim().equals("") ? false : match(CHINA_TELECOM_PATTERN, str);
			if (result1 || result2 || result3) {
				result = true;
			}
		}
		return result;
	}


	/**
	 * 执行正则表达式
	 *
	 * @param pat 表达式
	 * @param str 待验证字符串
	 * @return 返回true, 否则为false
	 */
	private static boolean match(String pat, String str)
	{
		Pattern pattern = Pattern.compile(pat);
		Matcher match = pattern.matcher(str);
		return match.find();
	}

	/**
	 * 校验身份证
	 *
	 * @param cardNo 身份证号码
	 * @return
	 */
	public static boolean isCard(String cardNo)
	{
		if (StringUtils.isEmpty(cardNo)) {
			return false;
		}
		Pattern p15 = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
		Pattern p18 = Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$");
		Matcher m15 = p15.matcher(cardNo);
		Matcher m18 = p18.matcher(cardNo);
		return m15.matches() || m18.matches();
	}

	/**
	 * 是否为空
	 *
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		if (str == null || "".equals(str)) {
			return true;
		}
		return false;
	}

	/**
	 * 是否不为空
	 *
	 * @param str
	 * @return true：不为空 false：为空
	 */
	public static boolean isNotEmpty(String str)
	{
		return !isEmpty(str);
	}

	/**
	 * 是否位数字
	 *
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str)
	{
		if (str != null) {
			Pattern p = Pattern.compile("^\\d+$");
			Matcher m = p.matcher(str);
			return m.matches();
		}
		return false;
	}

	/**
	 * 高级字符串替换
	 *
	 * @param str
	 * @param map
	 * @return
	 */
	public static String replace(String str, Map<Object, Object> map)
	{
		for (Object key : map.keySet()) {
			str = str.replace(key.toString(), map.get(key).toString());
		}
		return str;
	}

	/**
	 * 将字符串转字符串数组
	 *
	 * @param str       需要转换的字符串
	 * @param splitChar 分割符
	 * @return
	 */
	public static String[] strConvertArray(String str, String splitChar)
	{
		if (!StringUtils.isEmpty(str)) {
			if (!isEmpty(splitChar) && str.indexOf(splitChar) != -1) {
				return str.split(splitChar);
			}
			else {
				return new String[]{str};
			}
		}
		return new String[]{};
	}

	/**
	 * 将String转换成list<Integer>
	 *
	 * @param str       字符串
	 * @param splitChar 分隔符
	 * @return
	 */
	public static List<Integer> strConvertList(String str, String splitChar)
	{
		String strNub[] = strConvertArray(str, splitChar);
		List<Integer> listIds = new ArrayList<Integer>();
		if (strNub != null && strNub.length > 0) {
			for (int i = 0; i < strNub.length; i++) {
				listIds.add(Integer.valueOf(strNub[i].trim()));
			}
		}
		return listIds;
	}

	/**
	 * 将String转换成list<long>
	 *
	 * @param str       字符串
	 * @param splitChar 分隔符
	 * @return
	 */
	public static List<Long> strCntList(String str, String splitChar)
	{
		String strNub[] = strConvertArray(str, splitChar);
		List<Long> listIds = new ArrayList<Long>();
		if (strNub != null && strNub.length > 0) {
			for (int i = 0; i < strNub.length; i++) {
				listIds.add(Long.valueOf(strNub[i].trim()));
			}
		}
		return listIds;
	}

	/**
	 * 将String转换成list<String>
	 *
	 * @param str       字符串
	 * @param splitChar 分隔符
	 * @return
	 */
	public static List<String> strConvertListString(String str, String splitChar)
	{
		String strNub[] = strConvertArray(str, splitChar);
		List<String> listIds = new ArrayList<String>();
		if (strNub != null && strNub.length > 0) {
			for (int i = 0; i < strNub.length; i++) {
				listIds.add(strNub[i].trim());
			}
		}
		return listIds;
	}


	/**
	 * 字符串按指定长度截取并替换 @Description: 字符串按指定长度截取并替换 @param str 原字符串 @param length
	 * 截取长度 @param param 被截取部分替换的字符串 @return @return String @throws
	 */
	public static String strSplitReplace(String str, Integer length, String param)
	{
		if (isEmpty(str) || length == null || length > str.length())
			return str;

		str = str.substring(0, length);
		if (isNotEmpty(param)) {
			str = str + param;
		}

		return str;
	}

	/**
	 * @throws @author: VULCAN
	 * @Description: 截取后缀名
	 * @param: @param
	 * file
	 * @param: @return
	 * @return: String
	 * @date: 2017-2-8
	 */
	public static String getFormatForFile(MultipartFile file)
	{
		String imageName = file.getOriginalFilename();
		int lastIndex = imageName.lastIndexOf(".");
		return imageName.substring(lastIndex + 1, imageName.length());
	}

	/**
	 * @throws @author: VULCAN
	 * @Description: 生成Token
	 * @param: @return
	 * @return: String
	 * @date: 2016-12-30
	 */
	public static String getToken()
	{
		String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(30);
		for (int i = 0; i < 30; i++)
			sb.append(AB.charAt(rnd.nextInt(AB.length())));
		return sb.toString();
	}

	/**
	 * @throws @author: VULCAN
	 * @Description: 生成N位随机数
	 * @param: @return
	 * @return: String
	 * @date: 2017-2-10
	 */
	public static String getValidCode(Integer codeLength)
	{
		String validateCode = "";
		if (codeLength == null) {
			return "随机位数没传";
		}
		for (int i = 0; i < codeLength; i++) {
			BigDecimal num = new BigDecimal(Math.random() * 9).setScale(0, BigDecimal.ROUND_HALF_UP);
			validateCode += num;
		}
		return validateCode;
	}


	public static String getSplit(String str)
	{
		StringBuffer sb = new StringBuffer();
		String[] split = str.split("\\|");
		for (String string : split) {
			if (StringUtils.isNotEmpty(string)) {
				sb.append("|" + string);
			}
		}
		return sb.toString();
	}


	/**
	 * 商品富文本进行编辑 展示给APP页面
	 *
	 * @param html
	 * @return
	 */
	public static String getforMatHtml(String html)
	{
		StringBuffer head = new StringBuffer();
		head.append("<head>"
				+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
				+ "<script src=\"http://libs.baidu.com/jquery/2.0.0/jquery.js\"></script>\n" +
				"<script>\n" +
				"$(function(){\n" +
				"   $(\"img\").each(function(){\n" +
				"  $(this).removeAttr(\"width\");\n" +
				"  $(this).removeAttr(\"height\");\n" +
				"  $(this).removeAttr(\"style\");\n" +
				"         var c =$(this).width();\n" +
				"          var ch =$(this).height();\n" +
				"         var maxWidth = $(window).width();\n" +
				"       if(c>maxWidth){\n" +
				"$(this).css(\"maxWidth\",\"100%\");\n" +
//		            "             $(this).height(maxWidth* (ch / c));\n" +
				"         }\n" +
				"      else{ \n" +
				"$(this).css(\"width\",\"100%\"); }\n" +
				"     }); \n" +
				"});\n" +
				"</script>"
				+ "</head>");

		return "<html>" + head + "<body>" + html + "</body></html>";
	}

	public static String random(int max, String str)
	{
		Random r = new Random();
		return r.nextInt(max) + str;
	}

	/**
	 * 手机号码遮挡
	 *
	 * @param phone
	 * @return
	 */
	public static String phoneSing(String phone)
	{
		if (StringUtils.isNotEmpty(phone)) {
			StringBuilder sb = new StringBuilder(phone);
			String str = "****";
			sb.replace(3, 7, str);
			return sb.toString();
		}
		return null;

	}


	/**
	 * 根据特定拆分分隔符 拆分数组
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static String strArrayTostr(String split, String[] str)
	{
		StringBuilder sb = new StringBuilder();
		for (String temp : str) {
			sb.append(temp);
			sb.append(split);
		}

		return sb.toString();
	}


	/**
	 * 银行卡号遮挡
	 *
	 * @param card 银行卡号
	 * @return
	 */
	public static String bankCardSplit(String card)
	{
		if (StringUtils.isNotEmpty(card)) {
			StringBuilder sb = new StringBuilder(card);
			String str = " **** **** **** ";
			sb.replace(4, 15, str);
			return sb.toString();
		}
		return null;

	}


	/**
	 * 分页排序处理
	 *
	 * @param queryRequest
	 * @param page
	 * @return
	 */
	public static Page sortOrderBy(QueryRequest queryRequest, Page page)
	{
		if (StringUtils.isNotEmpty(queryRequest.getField()) && !queryRequest.getField().equals("undefined")) {
			String orderBy = camelToUnderscore(queryRequest.getField()) + " ";
			if (StringUtils.isNotEmpty(queryRequest.getOrder())
					&& !queryRequest.getOrder().equals("undefined")
					&& queryRequest.getOrder().equals(FfBootConstant.ORDER_ASC))
			{
				orderBy = orderBy + "asc";
			}
			else {
				orderBy = orderBy + "desc";
			}
			page.setOrderBy(orderBy);
		}
		return page;
	}

	public static Page sortOrderBy(QueryRequest queryRequest, Page page, String param)
	{
		if (StringUtils.isNotEmpty(queryRequest.getField()) && !queryRequest.getField().equals("undefined")) {
			String orderBy = camelToUnderscore(queryRequest.getField()) + " ";
			if (StringUtils.isNotEmpty(queryRequest.getOrder())
					&& !queryRequest.getOrder().equals("undefined")
					&& queryRequest.getOrder().equals(FfBootConstant.ORDER_ASC))
			{
				orderBy = orderBy + "asc";
			}
			else {
				orderBy = orderBy + "desc";
			}
			page.setOrderBy(param + orderBy);
		}
		return page;
	}

	/**
	 * 分页排序处理
	 *
	 * @param queryRequest
	 * @return
	 */
	public static String sortOrderBy(QueryRequest queryRequest)
	{
		if (queryRequest == null) {
			return null;
		}
		String orderBy = null;
		if (StringUtils.isNotEmpty(queryRequest.getField()) && !queryRequest.getField().equals("undefined")) {

			orderBy = camelToUnderscore(queryRequest.getField()) + " ";
			if (StringUtils.isNotEmpty(queryRequest.getOrder())
					&& !queryRequest.getOrder().equals("undefined")
					&& queryRequest.getOrder().equals(FfBootConstant.ORDER_ASC)) {
				orderBy = orderBy + "asc";
			}
			else {
				orderBy = orderBy + "desc";
			}
		}
		return orderBy;
	}

	/**
	 * 驼峰转下划线
	 *
	 * @param value 待转换值
	 * @return 结果
	 */
	public static String camelToUnderscore(String value)
	{
		if (StringUtils.isBlank(value))
			return value;
		String[] arr = StringUtils.splitByCharacterTypeCamelCase(value);
		if (arr.length == 0)
			return value;
		StringBuilder result = new StringBuilder();
		IntStream.range(0, arr.length).forEach(i ->
		{
			if (i != arr.length - 1)
				result.append(arr[i]).append(StringPool.UNDERSCORE);
			else
				result.append(arr[i]);
		});
		return StringUtils.lowerCase(result.toString());
	}

//    public static void main(String[] args)
//    {
//
//        String card="6235737000000525440";
//
//        System.out.println(strSplitReplace("6235 7370 0000 0525 440",4,"***********"));
//
//        System.out.println(bankCardSplit(card));
//
//    }

}
